<template>
  <el-dialog
    width="65%"
    :title="''"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    @close="resetForm"
  >
    <el-form
      ref="dataForm"
      size="mini"
      :model="dataForm"
      label-width="100px"
    >
      <div
        id="entrust"
        style="padding-left:25px;"
      >
        <p
          align="center"
          style="text-align:center;"
        >
          <strong><span style="font-size:15pt;font-family: 宋体;color:#00BFFF;">体育活动知情同意书及免责声明</span></strong>
        </p>
        <p>
          <span style="font-size:14pt;"><br></span>
        </p>
        <el-row
          type="flex"
          class="row-bg"
        >
          <el-col :span="24">
            <el-input
              v-model="dataForm.exceptionsInstructions"
              placeholder=""
              type="textarea"
              :autosize="{ minRows: 10, maxRows: 50}"
            />
          </el-col>
        </el-row>
      </div>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="visible = false"
      >取消</el-button>
      <el-button
        size="small"
        type="primary"
        :loading="loading"
        :disabled="!isClick"
        @click="dataFormSubmit()"
      >{{ text }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        text: '确定',
        isClick: true,
        countdown: 6,
        timer: null,
        dataForm: {
          dynamicPrescriptions:[],
          dynamicPrescriptionIndex: -1
        },
      }
    },
    mounted () {
    },
    methods: {
      init (dataForm) {
        this.visible = true
        this.dataForm = dataForm
        for(var i in this.dataForm.dynamicPrescriptions) {
            console.log(dataForm.dynamicPrescriptions[i])
            if(this.dataForm.dynamicPrescriptions[i].choose == "1") {
              this.$http({
                url: this.$http.requestUrl(`/sport/prescriptionexceptions/infoProjectName`),
                method: 'get',
                params: this.$http.requestParams({
                  'projectName':this.dataForm.dynamicPrescriptions[i].SportTypeName
                  })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.projectName = data.info.projectName
                  this.dataForm.exceptionsInstructions = data.info.exceptionsInstructions
                  this.time()
                }
              })
            }
        }
      },
      time() {
        if(this.isClick === false) {
          return;
        }
        this.isClick = false;
        this.timeLoading(); // 启动定时器
        this.timer = setInterval(() => {
          // 创建定时器
          if (this.countdown === 0 ) {
            // 设置的定时器时间为0后执行的操作
            this.timer && this.clearTimer() // 关闭定时器
          } else {
            this.timeLoading()
          }
        }, 1000)
      },
      timeLoading() {
        // 启动定时器
        this.countdown -= 1; //定时减1
        this.text = "确定(" + this.countdown + ")";
      },
      clearTimer() {
        // 清除定时器
        clearInterval(this.timer)
        this.timer = null;
        this.text = "确定"
        this.isClick = true
        this.countdown = 6
      },
      // 表单提交
      dataFormSubmit () {
        /*this.loading = true
        this.$http({
          url: this.$http.requestUrl(`/sport/prescription/saveSportPrescription`),
          method: 'post',
          data: this.$http.requestData({
            'sportProject': this.dataForm.sportProject,
            'sportDuration': this.dataForm.sportDuration,
            'sportFrequency': this.dataForm.sportFrequency,
            'sportIntensity': this.dataForm.sportIntensity
          })
        }).then(response => {
          const res = response.data
          if (res && res.code === 0) {
            this.visible = false
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$emit('success')
          } else {
            this.$message.error(res.msg)
          }
          this.loading = false
        })*/
        var tParent = this.$parent;
        this.$http({
          url: this.$http.requestUrl('/sport/prescription/v1/save'),
          method: 'post',
          data: JSON.stringify(this.dataForm.dynamicPrescriptions)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                console.log(this.closeByChild)
                //console.log(tParent.closeByChild)
                this.$emit('success')
                this.visible = false
              }
            })
          } else {
                  this.$message({
                    message: data.msg,
                  })
                }
        })
      },
      resetForm () {
        this.$refs['dataForm'].resetFields()
      }
    }
  }
</script>
<style>
  .entrust_style1{
    line-height:1.0em;
    vertical-align: baseline;
  }
  .entrust_style2{
    font-size:14pt;
    line-height:2.0em;
    font-family: 宋体;
    color: black;
  }
</style>
